﻿using SuperSocketServer.Command;
using SuperSocketServer.SSBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuperSocketServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (app != null && app.State == SuperSocket.SocketBase.ServerState.Running && textBox2.Text.Length > 0)
            {
                foreach (var s in app.GetAllSessions())
                {
                    new CMD_0004_DownText().push(s, textBox2.Text);
                }
            }
        }
        MyServer app;
        private void Form1_Load(object sender, EventArgs e)
        {
            var config = new SuperSocket.SocketBase.Config.ServerConfig()
            {
                Name = "SSServer",
                ServerTypeName = "SServer",
                ClearIdleSession = true, //60秒执行一次清理90秒没数据传送的连接
                ClearIdleSessionInterval = 60,
                IdleSessionTimeOut = 90,
                MaxRequestLength = 2048, //最大包长度
                Ip = "Any",
                Port = 18888,
                MaxConnectionNumber = 60000,
            };

            app = new MyServer();

            LogHelper.SetOnLog(new LogHelper.LogEvent((m) =>
            {

                textBox1.Invoke((Action)(() => {
                    textBox1.Text = string.Join(" ", m, "\r\n");
                    textBox1.Select(textBox1.TextLength, 0);
                    textBox1.ScrollToCaret();
                     }));
                //textBox1.Text = string.Join(" ", m, "\r\n");
                //textBox1.Select(textBox1.TextLength, 0);
                //textBox1.ScrollToCaret();

            }));
            app.Setup(config);
            if(!app.Start())
            {
                LogHelper.WriteLog(string.Format("Socket {0}:{1}启动失败，请检查权限或端口是否被占用！", config.Ip, config.Port));
            }
        }


    }
}
